package cn.net.wanji.job;

import cn.net.wanji.config.ApplicationConfig;
import cn.net.wanji.service.iml.DataReceiveServiceImpl;
import cn.net.wanji.utils.DiskUsagePercentage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

import cn.net.wanji.utils.FileUtils;

/**
 * 磁盘空间校验规则
 *
 * @author weining
 */
@Slf4j
@Component
public class CheckDiskJob {

    @Resource
    ApplicationConfig applicationConfig;

    @Scheduled(cron = "0 */30 * * * *")
    public void executeTask() {
        // 查看磁盘使用情况
        double diskUsagePercentage = DiskUsagePercentage.getDiskUsagePercentage(applicationConfig.getFileSavePath());
        if (diskUsagePercentage > Integer.parseInt(applicationConfig.getFileMemory())) {
            System.out.println("diskUsagePercentage==>当前磁盘使用了" + diskUsagePercentage);
            try{
                //执行删除数据的操作
                File earliestFolder = FileUtils.findEarliestFolder(applicationConfig.getFileSavePath());
                /*if (earliestFolder != null) {
                    System.out.println("最早的文件夹: " + earliestFolder.getAbsolutePath());
                } else {
                    System.out.println("未找到文件夹");
                }*/
                if (earliestFolder.exists()){
                    FileUtils.deleteFolder(earliestFolder);
                }
            }catch (Exception e){
                log.error("文件夹异常");
            }



        }

    }
}
